package com.lksoft.bz.action;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import com.lksoft.bz.Bz;
import com.lksoft.bz.StitchPrinter;

public class PrintAction extends AbstractAction{
	private static final long serialVersionUID = -5273526308221518279L;
	
	private Bz bz;
	
	/**
	 * @param bz The program
	 * @throws IOException 
	 */
	public PrintAction(Bz bz, String iconPath) throws IOException{
		super("", new ImageIcon(ImageIO.read(Bz.class.getResourceAsStream(iconPath)).getScaledInstance(32, 32, Image.SCALE_SMOOTH)));
		this.bz = bz;

		putValue(MNEMONIC_KEY, KeyEvent.VK_P);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		PrinterJob job = PrinterJob.getPrinterJob();
		job.setPrintable(new StitchPrinter(bz.getStitchPanel(), bz.getUsedBeads()), bz.getPageFormat());
		
		if( job.printDialog() ){
			try {
				job.print();
			} catch (PrinterException e1) {
				JOptionPane.showMessageDialog(bz.getFrame(), Bz.getUIString("messages.errors.cantprint"));
				e1.printStackTrace();
			}
		}
	}
}
